#!/bin/bash
#debug
set -x

yum update -y
yum install wget -y

cd /tmp
mkdir backup
mv /etc/yum.repos.d /tmp/backup/
cd /etc
mkdir yum.repos.d

cd yum.repos.d
wget -O /etc/yum.repos.d/CentOS-Base.repo  http://mirrors.aliyun.com/repo/Centos-7.repo
wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo
wget http://mirrors.aliyun.com/repo/Centos-7.repo

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
EOF

wget https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
rpm -import rpm-package-key.gpg

wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

yum clean all  && yum makecache fast

tee /etc/resolv.conf <<- 'EOF'
# Generated by NetworkManager
nameserver 8.8.8.8
nameserver 114.114.114.114
nameserver 223.5.5.5
EOF

yum install docker kubectl kubeadm kubelet -y

tee /etc/docker/daemon.json <<-'EOF'
    {
	 "registry-mirrors": ["https://17pfg8kv.mirror.aliyuncs.com"]
    }
EOF

#disable selinux
tee /etc/selinux/config  <<- 'EOF'
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
EOF

#swap off
swapoff -a

#ipvs
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4

#close firewall
service firewalld stop
systemctl disable firewalld
#enable docker
systemctl daemon-reload
systemctl enable docker
#enable kubelet
systemctl enable kubelet.service

echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables

#reboot
reboot

